install.packages('reticulate')
library(reticulate)
install_miniconda()Installation Guide
1 Foreword
For the class, installing either one of Positron or RStudio IDE will be sufficient, and there’s no need to install both. I’ll mostly be using Positron IDE for the class. Section 2 will be mostly relevant before coming to the first class.
1.1 What is IDE?
Integrated Development Environment (IDE), provides a user-friendly interface and convenient features for programming. Positron, RStudio, VSCode, PyCharm, Atom, Spyder, and Jupyter Lab are popular ones.
Positron IDE is a new code editor designed for both R & Python in the data science field. It is built on top of VSCode IDE and offers basic functionality of VSCode and seamless integration with R and Python. Currently open beta.
RStudio provides support for Python (with reticulate package), allowing users to effortlessly navigate between both languages.
1.2 R language
R is widely recognized and utilized by various professionals in fields such as data science, statistics, econometrics, and quantitative/financial analysis. Its popularity stems from its powerful capabilities and extensive range of packages for statistical analysis, big data analysis, visualization, and machine learning. I think (modern) R syntax is more streamlined, consistent and convenient for financial research.
1.3 Python language
Python is widely recognized and used in fields like computer science for general purpose and very popular in data science. Its popularity comes from its versatility, and a vast array of libraries that support tasks from data engineering to artificial intelligence - currently a lingua franca for machine learning.
2 Installations before the first class
If you are new to programming, please install R and Positron IDE on your computer before coming to the first class. I’ll guide you to install python later in the course if necessary.
2.1 Install R
Let’s download and install R.
- Navigate to the R CRAN webpage to download R client. Please download most recent R client that matches your OS.
2.1.1 Windows
- For Windows, choose ‘base’ sub-directories to download R.
base for WindowsIt is recommended that users adhere to the default settings during installation unless they are experienced with environment variable settings (path). Modifying these settings without sufficient familiarity can potentially lead to time-consuming issues or complications.
2.1.2 MacOS
For Mac, choose R-4.x.x.pkg file. Make sure to download proper build for your mac (Intel or Apple silicon(M1, M2, etc)).
It is recommended that users adhere to the default settings during installation unless they are experienced with environment variable settings (path). Modifying these settings without sufficient familiarity can potentially lead to time-consuming issues or complications.
For macOS, install R package by executing .pkg file.
2.2 Install Positron IDE
Positron IDE is currently in open beta and available to download from GitHub. To navigate, follow the link: https://github.com/posit-dev/positron/releases.
Click Assets to open up the dropdown menu and download proper installer for your OS.
.exe file for Windows users
.dmg file for MacOS
.deb for linux
3 Other Installations
3.1 Install Python (Vanilla)
There are numerous ways to download and install python, perhaps you already have python installed on your computer (e.g., MacOS / Linux).
I introduce the basic way. Navigate to Python webpage and download python installer, then install.
If you are on Mac and if you have already installed Apple command-line tools (if you are familiar with the below), you can skip Python installation.
3.2 Install Quarto
To install Quarto, Navigate to Quarto.org and download installer.
3.3 Install RStudio
Navigate to RStudioIDE Stable Build to get the stable version of RStudio IDE. Download installers that matches your OS from RStudio Desktop section.
3.3.1 RStudio Installation - Windows
The installation process of RStudio on Windows is typically straightforward.
If you have successfully installed R and Rstudio, you will see Figure 1 on your machine.
3.3.2 Reticulate-Python in RStudio
We will install Python using the R package called reticulate, which allows for seamless integration of R and Python within the RStudio IDE.
Please execute the following commands in your R console.
If the installation mentioned above is successful, your Python installation is now complete.
- For Windows machines, you will notice that Anaconda Prompt has been added to the start menu. You can utilize this prompt (terminal) going forward to install Python packages and manage your environments.
3.3.3 Setting Python Path for Windows (Optional)
If you already have Python installed on your machine, you can specify the Python path to use it instead of installing a new version. If this is the case, make sure to provide your own path. However, it is recommended to install Python from the reticulate package to avoid potential complications.
For Windows users, follow these steps:
- Copy paste below code on your
command prompt. Then exit and re-launch RStudio.
echo RETICULATE_PYTHON = "%userprofile%\AppData\Local\r-miniconda\envs\r-reticulate\python.exe" >> %userprofile%\Documents\.Renviron
- Then open a python script (.py) and run the empty code to activate python console.
- Then Go to Tools > Global Options and set python interpreter for RStudio. Make sure you are choosing python from
conda environmentsand choose python underr-reticulateenvironment as below.
3.3.4 RStudio Installation - macOS
Installing R and RStudio on macOS is typically a straightforward process. It is recommended to proceed with the default settings and avoid making any changes unless you have a specific reason or are knowledgeable about the implications of modifying certain settings.
- For RStudio, drag and drop the RStudio application to your
applicationsfolder.
If you have successfully installed R and Rstudio, you will see below on your machine.
3.3.4.1 Apple Command Line Tools
When launching RStudio for the first time on macOS, it may recommend downloading Apple Command Line Tools. It is highly beneficial to install this tool on your machine, as it offers various advantages. The installation process takes approximately 30 minutes.
3.3.4.2 Reticulate-Python in RStudio
To install Python and enable seamless integration with R in the RStudio IDE, we will use the R package called reticulate.
Please run the following command in your R console:
install.packages('reticulate')
library(reticulate)
install_miniconda()On macOS, the operating system already includes a built-in Python 3 installation that comes with Apple Command Line Tools. However, we will not be using this Python installation. Instead, we will utilize the Miniconda Python installation that was installed from above steps.
To use
condain the Terminal on macOS, it needs to be activated and initialized. Follow these steps by copying and pasting the commands below into your Terminal, one line at a time:Tip: You can access Terminal from your Launchpad or use the spotlight search by pressing
Cmd+Spacebarand searching for Terminal.
3.3.4.3 Step 1
source ~/Library/r-miniconda/bin/activate- If you’re using Apple Silicon Mac, then name of miniconda directory will be slightly different. Use this code instead of above if this being the case.
source ~/Library/r-miniconda-arm64/bin/activate # use this command instead if Apple silicon
3.3.4.4 Step 2
Then, type command below. This will initialize conda on your zsh.
conda init zshIf you are using
bashas your default shell, then useconda init bash. Your shell name is on the top of the terminal.If you see (base) on the left of your prompt, then your Terminal is set for conda!
3.3.4.5 Set Python Path for Mac (Optional)
Please copy and paste the following command into your terminal. This command will generate the settings file. After executing the command, exit and relaunch RStudio.
echo 'RETICULATE_PYTHON="~/Library/r-miniconda/envs/r-reticulate/bin/python"' >> ~/.Renviron
4 Troubleshooting
If, for any reason, the Python installation process described earlier does not work as expected, please refer to this section for troubleshooting steps. This section will be regularly updated with the latest information to assist you in resolving any installation issues.
4.1 Download time limit
If you are experiencing issues with R automatically disconnecting when downloading due to a slow or weak internet connection, you can apply a temporary fix by changing the timeout setting in R.
To do this, simply type the following command in your R console:
options(timeout=500) # setting timeout for 500 secsTo permanently change the internet timeout setting, follow the instructions based on your operating system:
For Windows, open the Command Prompt and execute the following command:
echo R_DEFAULT_INTERNET_TIMEOUT=500 >> %userprofile%\Documents\.Renviron
For macOS, open the Terminal and execute the following command:
echo "R_DEFAULT_INTERNET_TIMEOUT=500" >> ~/.Renviron
This command appends the specified configuration to your .Renviron file located in the Documents folder of your user profile, setting the internet timeout to 500 seconds.
4.2 Windows
4.2.1 Error installing miniconda due to whitespace
In windows, it is reported that when username has blank space such as "Ryan Gonzalez" , miniconda is not installed on the default path. In this case, user has to specify a different folder address where there is no blank space, such as C:\r-miniconda.
To do this, on R Prompt type this command.
reticulate::install_miniconda("C:\r-miniconda")Then set python path for R Environment by typing below on command prompt.
echo RETICULATE_PYTHON = "C:\r-miniconda\envs\r-reticulate\python.exe" >> %userprofile%\Documents\.Renviron
4.2.2 Manually Setting up Anaconda Prompt
If anaconda prompt is not installed on your machine, we can manually add the prompt.
Create a shortcut (i.e. symlink) for command prompt on your desktop.
Go to its properties and change target as
%windir%\System32\\cmd.exe "/K" %userprofile%\AppData\Local\R-MINI\~1\Scripts\activate.batchange Start in as
%HOMEPATH%and paste the shortcut to a folder with proper folder name located at
%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
4.3 MacOS
4.3.1 Reticulate path setting
Sometimes Rstudio does not locate the miniconda path automatically. In this case, user can manually set the path for Python and can tell R where it is installed.
4.3.2 Apple Silicon models
If miniconda installed by reticulate is x86_64 architecture, below error message will show:
Error in py_initialize(config$python, config$libpython, config$pythonhome, :
: no suitable image found. Did find:
: mach-o, but wrong architecture
Instead, miniforge offers arm64(Apple Silicon) architecture, so I’m introducing this miniforge workaround.
- Go to Miniforge3 webpage and download
Miniforge3-MacOSX-arm64.
It will be downloaded in the ~/Downloads.
Then type below command on your terminal to install
zsh \~/Downloads/Miniforge3-MacOSX-arm64.sh
miniforge3 will be installed on ~/miniforge3 by default. Answer yes to agree and another yes for initiating conda.
- Then, let’s make a new virtual environment1 called
r-reticulatefor consistency. Type below on your terminal:
conda create -n r-reticulate python
- Next, let’s set python path such that RStudio may find the path where
miniforge3is installed. Type below command on terminal:
echo 'RETICULATE_PYTHON="\~/miniforge3/envs/r-reticulate/bin/python"' >> ~/.Renviron
Now you’re ready to go.
4.4 Installing python packages
To install Python packages in R using the reticulate package, you have two convenient options: py_install() and conda_install() functions.
conda_install('r-reticulate','pandas') # install pandas package to our reticulated python environment
py_install('pandas') # will install pandas to reticulated python by defaultThe virtual environment of python we will be using is r-reticulate, which is a python env that is connected to the RStudio. We will be installing python packages on this environment.
To achieve this, we should activate our conda environment as r-reticulate by using command below on conda prompt.
conda activate r-reticulate
then let’s install python packages using command pip install. Or you can use conda install alternatively. Below packages are what we will be using throughout the semester.
pip install pandas numpy scipy numpy-financial xlwings mplfinance yfinance matplotlib jupyterlab
4.5 data.table Installation
The default installation (a.k.a. pre-compiled, or (compiled) binary version) of data.table package from CRAN for mac is not built with openMP-enabled compiler. This installation still works, but it forces to use 1 thread, which can limit its performance.
To install the package properly, you will need to compile it on your own mac, with openMP-enabled C compiler.
- Install homebrew to install C compiler for your mac.
Go to https://brew.sh and install homebrew by copy-pasting code on your terminal. It will require your mac password to install.
If homebrew asks you to run commands as next step, follow them. At this moment of writing, brew suggests to run below codes to add Homebrew to your PATH. If this is not done, it will not understand
brewcommand.Homebrew next step Following the next step
- We will install some scaffolding packages and
gccas our compiler. Runbrew install gcc pkg-configon your terminal.
Check out the version of
gccinstalled. Typebrew info gcc. At the moment of writing, the currentgccstable version is13.1.0(or simply13).Now we have to tell R to use this
gccfor package compilation. Typemkdir ~/.Randnano ~/.R/Makevarson your terminal (Case sensitive! Make sure to type correctly, or copy paste). If successful, you will see a text editor. This is needed so that you may not accidentally create an.rtftext file.Copy paste below code on your terminal, and save it. Note that
GCC_VERis set to13as we verified in step 3. If the version number differs, set it accordingly. Exit by hitting^Xand save the file.
- ARM based macs
LOC = /opt/homebrew
#/opt/homebrew for ARM Macs (M1 and its successors)
GCC_VER=13
CC=$(LOC)/bin/gcc-$(GCC_VER) -fopenmp
CXX=$(LOC)/bin/g++-$(GCC_VER) -fopenmp
CXX11=$(LOC)/bin/g++-$(GCC_VER) -fopenmp # for fst package
CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS += -L$(LOC)/opt/libomp/lib -lomp
CPPFLAGS += -I$(LOC)/opt/libomp/include -fopenmp
- Intel based macs
LOC = /usr/local
#/usr/local for Intel Macs
GCC_VER=13
CC=$(LOC)/bin/gcc-$(GCC_VER) -fopenmp
CXX=$(LOC)/bin/g++-$(GCC_VER) -fopenmp
CXX11=$(LOC)/bin/g++-$(GCC_VER) -fopenmp # for fst package
CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(LOC)/lib -Wl,-rpath, -I$(LOC)/lib
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
- Close RStudio and open it again. Then install
data.tablepackage with following code.
install.packages("data.table", type = "source",
repos = "https://Rdatatable.gitlab.io/data.table")- To confirm a successful installation, when you load
data.table, you will see it is using half of your number of cores as default.
5 Uninstall
5.1 macOS R uninstallation
Below code completely removes R, RStudio and miniconda (reticulate) installed on your computer. Use it when you’d like to remove and reinstall R/RStudio. You’ll need to provide password on the prompt.
sudo rm -rf /Applications/R.app
sudo rm -rf /Applications/RStudio.app
sudo rm -rf /Library/Frameworks/R.framework
sudo rm -rf /Library/Saved Application state/org.rstudio*
sudo rm -rf ~/.config/rstudio
sudo rm /usr/local/bin/{R,Rscript}
sudo rm /private/var/db/receipts/org.R-project*
sudo rm /private/var/db/receipts/org.r-project*
sudo rm /private/var/db/receipts/org.rstudio*
rm -rf ~/Library/Application Support/R
rm -rf ~/.Renviron
conda deactivate
sudo rm -rf ~/Library/r-miniconda/
rm -rf ~/.zshrc
rm -rf ~/.bashrc
6 Option settings
6.1 RStudio Workspace setup
Usually it is not recommended to load workspace in the beginning of the session, so let’s turn off this feature. Uncheck Restore .RData into workspace at startup and set Never to save workspace to .RData on exit.
6.2 RStudio Terminal setup for MacOS
- For MacOS, match the default shell (zsh) as RStudio’s default.
Footnotes
In simple terms, a virtual environment is like creating a copy of your Python installation to preserve the original setup.↩︎